package com.emex.app.stuff.gui;

import com.emex.app.shared.data.Client;
import com.emex.app.stuff.gui.model.StaffTableModel;
import java.util.List;

/**
 *
 * @author Sergey Karavaev
 */
public class SelectClientDialog extends javax.swing.JDialog {

    private Client client;

    /**
     * Creates new form SelectClientDialog
     */
    public SelectClientDialog(java.awt.Frame parent, boolean modal, List<Client> foundClients) {
        super(parent, modal);
        initComponents();
        StaffTableModel tableModel = new StaffTableModel(foundClients);
        clientsTable.setModel(tableModel);
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public Client getClient() {
        return client;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        clientsTable = new org.jdesktop.swingx.JXTable();
        chooseClientButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(org.openide.util.NbBundle.getMessage(SelectClientDialog.class, "SelectClientDialog.title")); // NOI18N

        clientsTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                clientsTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(clientsTable);

        org.openide.awt.Mnemonics.setLocalizedText(chooseClientButton, org.openide.util.NbBundle.getMessage(SelectClientDialog.class, "SelectClientDialog.chooseClientButton.text")); // NOI18N
        chooseClientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseClientButtonActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(cancelButton, org.openide.util.NbBundle.getMessage(SelectClientDialog.class, "SelectClientDialog.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 364, Short.MAX_VALUE)
                        .addComponent(chooseClientButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(chooseClientButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void chooseClientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseClientButtonActionPerformed
        client = getClientSelection();
        this.setVisible(false);
    }//GEN-LAST:event_chooseClientButtonActionPerformed

    private void clientsTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clientsTableMouseClicked
        if (evt.getClickCount() == 2) {
            client = getClientSelection();
            setVisible(false);
        }
    }//GEN-LAST:event_clientsTableMouseClicked

    protected Client getClientSelection() {
        if (clientsTable.getSelectedRow() > -1) {
            Client clientSel = (Client) clientsTable
                    .getValueAt(clientsTable.getSelectedRow(), -1);
            return clientSel;
        }
        return null;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseClientButton;
    private org.jdesktop.swingx.JXTable clientsTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
